/**
 * @file bsp_usb.h
 * @brief USB CDC driver for H743-V2 Flight Control System
 * @author Flight Control Team
 * @date 2025-10-31
 */

#ifndef __BSP_USB_H
#define __BSP_USB_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>

/**
 * @brief USB connection state
 */
typedef enum {
    USB_STATE_DISCONNECTED = 0,
    USB_STATE_CONNECTED,
    USB_STATE_CONFIGURED,
    USB_STATE_SUSPENDED
} USBState_t;

/**
 * @brief USB CDC line coding structure
 */
typedef struct {
    uint32_t baudrate;
    uint8_t  data_bits;
    uint8_t  parity;
    uint8_t  stop_bits;
} USBLineCoding_t;

/**
 * @brief Initialize USB CDC
 * @return 0 on success, -1 on error
 */
int BSP_USB_Init(void);

/**
 * @brief Deinitialize USB CDC
 */
void BSP_USB_DeInit(void);

/**
 * @brief Get USB connection state
 * @return USB connection state
 */
USBState_t BSP_USB_GetState(void);

/**
 * @brief Check if USB is connected
 * @return true if connected, false otherwise
 */
bool BSP_USB_IsConnected(void);

/**
 * @brief Send data via USB CDC
 * @param data Pointer to data buffer
 * @param len Length of data
 * @return Number of bytes sent, -1 on error
 */
int BSP_USB_Send(const uint8_t *data, uint16_t len);

/**
 * @brief Send string via USB CDC
 * @param str Null-terminated string
 * @return Number of bytes sent, -1 on error
 */
int BSP_USB_SendString(const char *str);

/**
 * @brief Receive data from USB CDC
 * @param data Pointer to data buffer
 * @param len Maximum length to receive
 * @return Number of bytes received, -1 on error
 */
int BSP_USB_Receive(uint8_t *data, uint16_t len);

/**
 * @brief Get number of bytes available in receive buffer
 * @return Number of bytes available
 */
uint16_t BSP_USB_Available(void);

/**
 * @brief Flush transmit buffer
 */
void BSP_USB_Flush(void);

/**
 * @brief Printf-like function for USB CDC
 * @param format Format string
 * @param ... Variable arguments
 * @return Number of bytes sent
 */
int BSP_USB_Printf(const char *format, ...);

/**
 * @brief USB CDC receive callback (called when data is received)
 * @param data Pointer to received data
 * @param len Length of received data
 */
void BSP_USB_ReceiveCallback(uint8_t *data, uint16_t len);

#ifdef __cplusplus
}
#endif

#endif /* __BSP_USB_H */

